<template>
    <div>
        <el-alert title="Tips:点击【新增】按钮进行新增商品；点击【编辑】按钮，进行对商品的编辑操作！" type="warning"> </el-alert>
        <el-alert title="Tips:查看商品的发货地址可以把鼠标放在该商品名称上即可显示，若商品描述过长会自动隐藏 显示操作同前" type="warning"> </el-alert>
        <el-card class="card-ctrl">
            <el-row>
                <el-col :span="8" style="text-align: left">
                    <el-button type="primary" size="small" @click="onCreate">
                        <el-icon>
                            <plus />
                        </el-icon>
                        新增
                    </el-button>
                    <el-button type="success" size="small" @click="onRefresh2">
                        <el-icon>
                            <Refresh />
                        </el-icon>
                        刷新
                    </el-button>
                </el-col>
            </el-row>
            <br />
            <el-table :data="goodsdata" style="width: 100%">
                <el-table-column label="商品名称" width="180" fixed>
                    <template #default="scope">
                        <el-popover effect="light" trigger="hover" placement="top" width="auto">
                            <template #default>
                                <div>商品名称: {{ scope.row.goodsName }}</div>
                                <div>发货地址: {{ scope.row.goodsAddress }}</div>
                            </template>
                            <template #reference>
                                <span style="margin-left: 10px">{{ scope.row.goodsName }}</span>
                            </template>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="商品类型" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <el-tag>{{ scope.row.typeName }}</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="单价" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span style="margin-left: 10px">{{ scope.row.goodsPrice }}元</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="库存" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span style="margin-left: 10px">{{ scope.row.goodsAmount }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="商品描述" width="200" :show-overflow-tooltip='true'>
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <span>{{ scope.row.goodsDetail }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="商品状态" width="180">
                    <template #default="scope">
                        <div style="display: flex; align-items: center">
                            <el-tag v-if="scope.row.goodsState == 1">上架</el-tag>
                            <el-tag v-else>下架</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" style="" width="250">
                    <template #default="scope">
                        <el-button size="small" v-if="scope.row.goodsState == 1"
                            @click="added(scope.$index, scope.row)" type="primary">下架</el-button>
                        <el-button v-else size="small" @click="unshelve(scope.$index, scope.row)" type="primary">上架</el-button>
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)" type="primary">编辑</el-button>
                        <el-button size="small" type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 新增商品的弹出对话框 -->
        <el-dialog v-model="add_visible" title="新增商品" :before-close="handleClose">
            <el-form :model="addform" label-width="120px">

                <el-form-item label="商品名称">
                    <el-input v-model="addform.goodsName" />
                </el-form-item>

                <el-form-item label="商品类型">
                    <el-select v-model="addform.typeId" placeholder="请选择商品的类型">
                        <el-option v-for="item in typedata" :key="item.typeId" :label="item.typeName"
                            :value="item.typeId" />
                    </el-select>
                </el-form-item>

                <el-form-item label="商品价格">
                    <el-input v-model.number="addform.goodsPrice" type="number" />
                </el-form-item>

                <el-form-item label="商品发货地址">
                    <el-input v-model="addform.goodsAddress" />
                </el-form-item>

                <el-form-item label="商品库存">
                    <el-input v-model.number="addform.goodsAmount" type="number" />
                </el-form-item>

                <el-form-item label="商品描述">
                    <el-input v-model="addform.goodsDetail" type="textarea" />
                </el-form-item>

                <el-form-item label="商品主图">
                    <el-upload ref="upload" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                        :limit="1" :on-exceed="handleExceed" :auto-upload="false">
                        <template #trigger>
                            <el-button type="primary">上传</el-button>
                        </template>
                        <template #tip>
                            <div>
                                限制1个文件，新文件将覆盖旧文件
                            </div>
                        </template>
                    </el-upload>
                </el-form-item>

            </el-form>
            <template #footer>
                <span>
                    <el-button @click="add_visible = false">取消</el-button>
                    <el-button type="success" @click="addgoods">
                        确认
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <!-- 修改商品的弹出对话框 -->
        <el-dialog v-model="alter_visible" title="修改商品" :before-close="handleClose">checkdata
            <el-form :model="addform" label-width="120px">

                <el-form-item label="商品名称">
                    <el-input v-model="addform.goodsName" />
                </el-form-item>

                <el-form-item label="商品类型">
                    <el-select v-model="addform.typeId" placeholder="请选择商品的类型">
                        <el-option v-for="item in typedata" :key="item.typeId" :label="item.typeName"
                            :value="item.typeId" />
                    </el-select>
                </el-form-item>

                <el-form-item label="商品价格">
                    <el-input v-model.number="addform.goodsPrice" type="number" />
                </el-form-item>

                <el-form-item label="商品发货地址">
                    <el-input v-model="addform.goodsAddress" />
                </el-form-item>

                <el-form-item label="商品库存">
                    <el-input v-model.number="addform.goodsAmount" type="number" />
                </el-form-item>

                <el-form-item label="商品描述">
                    <el-input v-model="addform.goodsDetail" type="textarea" />
                </el-form-item>

                <el-form-item label="商品主图">
                    <el-upload ref="upload" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                        :limit="1" :on-exceed="handleExceed" :auto-upload="false">
                        <template #trigger>
                            <el-button type="primary">上传</el-button>
                        </template>
                        <template #tip>
                            <div>
                                限制1个文件，新文件将覆盖旧文件
                            </div>
                        </template>
                    </el-upload>
                </el-form-item>

            </el-form>
            <template #footer>
                <span>
                    <el-button @click="alter_visible = false;">取消</el-button>
                    <el-button type="success" @click="updategoods">
                        确认
                    </el-button>
                </span>
            </template>
        </el-dialog>


    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, h } from "vue";
import { ElMessageBox, genFileId, ElMessage } from 'element-plus'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
import { useStore } from '../store/store'
import { checktype, create, refreshGoods, addedGoods, unshelveGoods, deleteGoods, alterGoods } from '../api/goods.js'


const goodsdata = ref();
const add_visible = ref(false);
const alter_visible = ref(false);


const addform = reactive({
    goodsName: '',
    typeId: '',
    goodsAddress: '',
    goodsPrice: '',
    goodsAmount: '',
    goodsDetail: '',
    goodsPicture: '',
    goodsId:0,
    StoreId:useStore.storeId
})

const typedata = ref();
const upload = ref<UploadInstance>()


const handleExceed: UploadProps['onExceed'] = (files) => {
    upload.value!.clearFiles()
    const file = files[0] as UploadRawFile
    file.uid = genFileId()
    upload.value!.handleStart(file)
}



/*
    进入页面调用的方法
*/
onMounted(() => {
    onRefresh();
})

/*
    获取商品类型方法
*/
function gettype() {
    checktype()
        .then(function (res) {
            typedata.value = res.data;
        })
        .catch(function (error) {
            console.log(error);
        });
}

/*
    新增按钮方法
*/
function onCreate() {
    gettype();
    addform.goodsAddress = '';
    addform.goodsAmount = '';
    addform.goodsDetail = '';
    addform.goodsName = '';
    addform.goodsPicture = '';
    addform.goodsPrice = '';
    addform.typeId = '';
    add_visible.value = true;
}

/*
    确认新增商品方法
*/
function addgoods() {
    add_visible.value = false;
    console.log(create(addform));
    ElMessage({
        showClose: true,
        message: '新增商品成功',
        type: 'success',
    })
    alert("确认新增" + addform.typeId + upload.value);
    onRefresh();
}

/*
    确认修改商品方法
*/
function updategoods() {
    alter_visible.value = false;
    console.log(alterGoods(addform));
    ElMessage({
        showClose: true,
        message: '商品修改成功',
        type: 'success',
    })
    alert("确认新增" + addform.typeId + upload.value);
    onRefresh();
}

/*
    新增弹出框的关闭方法
*/
function handleClose() {
    ElMessageBox.confirm('是否取消?(该条信息将不会保存)')
        .then(() => {
            add_visible.value = false;
            alter_visible.value = false;
        })
        .catch(() => {
            // catch error
        })
}

/*
    刷新方法
 */
function onRefresh() {
    refreshGoods(1)
        .then(function (res) {
            goodsdata.value = res.data;
        })
        .catch(function (error) {
            console.log(error);
        });
}

/*
    刷新按钮方法
 */
function onRefresh2() {
    onRefresh();
    ElMessage({
        showClose: true,
        message: '刷新成功',
        type: 'success',
    })
}

/*
    编辑按钮方法
*/
function handleEdit(index, row) {
    console.log(row);
    gettype();
    addform.goodsAddress = row.goodsAddress;
    addform.goodsAmount = row.goodsAmount;
    addform.goodsDetail = row.goodsDetail;
    addform.goodsName = row.goodsName;
    addform.goodsPicture = row.goodsPicture;
    addform.goodsPrice = row.goodsPrice;
    addform.typeId = row.typeId;
    addform.goodsId = row.goodsId;;
    alter_visible.value = true;
}

/*
    删除按钮方法
*/
function handleDelete(index, row) {
    ElMessageBox.confirm('是否删除商品?')
        .then(() => {
            deleteGoods(row.goodsId)
                .then(function (res) {
                    ElMessage.error('删除商品成功');
                    onRefresh();
                })
                .catch(function (error) {
                    console.log(error);
                });
        })
        .catch(() => {
            // catch error
        })
}

/*
    上架按钮方法
*/
function added(index, row) {
    unshelveGoods(row.goodsId)
        .then(function (res) {
            ElMessage({
                showClose: true,
                message: '商品上架成功',
                type: 'success',
            })
            onRefresh();
        })
        .catch(function (error) {
            console.log(error);
        });
}

/*
    下架按钮方法
*/
function unshelve(index, row) {
    addedGoods(row.goodsId)
        .then(function (res) {
            ElMessage({
                showClose: true,
                message: '商品下架成功',
                type: 'success',
            })
            onRefresh();
        })
        .catch(function (error) {
            console.log(error);
        });
}



</script>

<style scoped>
.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>